﻿using System;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;

namespace CommonUtils
{
    /// <summary>
    /// Html处理
    /// </summary>
    public static class HtmlUtil
    {
        /// <summary>
        /// 换行转为br
        /// </summary>
        public static string ToHtml(this string text)
        {
            if (text.StartsWith('<'))
                return text;
            string html = text.Replace(" ", "&nbsp;");
            html = html.Replace("\r\n", "<br />");
            html = html.Replace("\n", "<br />");
            return html;
        }

        public static string ToHtml(this object value)
        {
            if (value == null)
                return null;
            return value.ToString().ToHtml();
        }

        public static string ToHtml(this string[] lines)
        => lines.JoinToText().ToHtml();

        /// <summary>
        /// 通过数组获取Table的Html代码
        /// </summary>
        public static string ToHtml<T>(this T[] array, string css = "table")
        {
            var html = new StringBuilder();
            html.AppendLine("<table class=\"{0}\">", css);
            var properties = typeof(T).GetProperties();
            html.Append("<tr>");
            foreach (var property in properties)
            {
                html.Append("<th>");
                html.Append(property.Name);
                html.Append("</th>");
            }
            html.AppendLine("</tr>");
            foreach (var item in array)
            {
                html.Append("<tr>");
                foreach (var property in properties)
                {
                    html.Append("<td>");
                    html.Append(property.GetValue(item).ToHtml());
                    html.Append("</td>");
                }
                html.AppendLine("</tr>");
            }
            html.Append("</table>");
            return html.ToString();
        }

        public static string ToHtml(this DataTable table, string css = "table")
        {
            var html = new StringBuilder();
            html.AppendLine("<table class=\"{0}\">", css);
            html.Append("<tr>");
            foreach (DataColumn column in table.Columns)
            {
                html.Append("<th>");
                html.Append(column.ColumnName);
                html.Append("</th>");
            }
            html.AppendLine("</tr>");
            foreach (DataRow row in table.Rows)
            {
                html.Append("<tr>");
                foreach (DataColumn column in table.Columns)
                {
                    html.Append("<td>");
                    html.Append(row[column].ToHtml());
                    html.Append("</td>");
                }
                html.AppendLine("</tr>");
            }
            html.Append("</table>");
            return html.ToString();
        }

        /// <summary>
        /// 过滤html代码,保留文字字符
        /// </summary>
        public static string FilterTag(string html)
        {
            html = html.Trim();
            return string.IsNullOrEmpty(html) ? string.Empty : Regex.Replace(html, "<[^>]*>", string.Empty);
        }

        /// <summary>
        /// 获取用于显示的html代码
        /// </summary> 
        public static string GetDisplayXml(string html)
        => html.Replace("<", "&lt;").Replace(">", "&gt;");

        /// <summary>
        /// <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        /// </summary>
        public const string CharsetMeta = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />";

        /// <summary>
        /// ViewPort <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        /// </summary>
        public const string ViewPortScaleMeta = "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />";

        /// <summary>
        /// 网页重定位,尽量填写/绝对路径 <meta http-equiv="refresh" content="0;url=/test" />
        /// </summary>
        public static string GetRelocalMeta(string url)
        {
            if (string.IsNullOrEmpty(url))
                return null;
            return string.Format("<meta http-equiv=\"refresh\" content=\"0;url={0}\" />", url);
        }

        /// <summary>
        /// ICON
        /// </summary>
        public const string IconLink = "<link href=\"/favicon.ico\" mce_href=\"/favicon.ico\" rel=\"shortcut icon\" type=\"image/x-icon\" />";

        public static string GetIconLink(string path)
        => IconLink.Replace("/favicon.ico", path);

        public static string Title(string title) => "<title>" + title + "</title>";

        /// <summary>
        /// 把字符串转换为UnicodeHtml
        /// </summary>
        public static string CompletePage(string innerBoday)
        {
            var html = "<!DOCTYPE html><html><head>{0}</head><body>{1}</body></html>";
            return string.Format(html, CharsetMeta, innerBoday);
        }

        /// <summary>
        /// 添加htmlA标签
        /// </summary> 
        public static string Href(string url, bool blank = true, string content = null)
        {
            url = UrlUtil.Format(url);
            return string.Format("<a href=\"{0}\"{1}>{2}</a>", url, blank ? " target=\"_blank\"" : null, content);
        }

        /// <summary>
        /// 添加htmlA标签
        /// </summary> 
        public static string Href(string[] urls, bool blank = true)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string url in urls)
                sb.Append(string.Format("<a href=\"{0}\"{1}>{0}</a><hr />", url, blank ? " target=\"_blank\"" : null));
            return sb.ToString();
        }

        /// <summary>
        /// 邮件链接
        /// </summary>
        public static string MailHref(string mail, string content = null)
        {
            if (string.IsNullOrEmpty(content))
                content = mail;
            string html = "<a href=\"mailto:{0}\">{1}</a>";
            return string.Format(html, mail, content);
        }

        /// <summary>
        /// QQ链接
        /// </summary>
        public static string QQUrl(string qq)
        => "tencent://message/?uin=" + qq;

        /// <summary>
        /// QQ链接
        /// </summary>
        public static string QQHref(string qq, string content = null)
        {
            if (string.IsNullOrEmpty(content))
                content = qq;
            return string.Format("<a href=\"{0}\">{1}</a>", QQUrl(qq), content);
        }

        /// <summary>
        /// 网页背景音乐
        /// </summary>
        public static string Audio(string virPath = "/audio/bgm.mp3", bool display = true, bool autoplay = true)
        {
            /*
            <audio autoplay="autoplay" controls="controls" loop="loop" preload="auto" src="assets/Log-in-long2.wav" style="position:fixed;left:0;top:0">
            <source src="assets/Log-in-long2.wav" />
            Your browser does not support the audio element.
            </audio>
            */
            var sb = new StringBuilder();
            sb.AppendFormat("<audio {2}controls=\"controls\" loop=\"loop\" preload=\"auto\" src=\"{0}\" style=\"position:fixed;left:0;top:0;{1}\">", virPath, display ? null : "display:none", autoplay ? "autoplay=\"autoplay\" " : null);
            sb.AppendFormat("<source src=\"{0}\" />", virPath);
            sb.Append("Your browser does not support the audio element.");
            sb.Append("</audio>");
            return sb.ToString();
        }

        public static string Audio_(string virPath = "/audio/bgm.mp3", bool display = true, bool autoplay = true)
        {
            /*
            <video controls="" autoplay="" name="media"><source src="http://localhost:5000/audio/Log-in-long2.wav" type="audio/wav"></video>
            */
            return "<video controls autoplay name='media'><source src='" + virPath + "' type='audio/wav'></video>";
        }

        public static string Png_(string base64, int width = 100, int height = 100)
        => "<img width='" + width + "' height='" + height + "' src='data:image/png;base64," + base64 + "' />";

        public static string Png(string path, int width = 100, int height = 100)
        => Png_(FileUtil.GetBase64(path));

        public static string PngNoPic(int width = 100, int height = 100)
        => Png_(ResourceUtil.NoPicBase64());

        public static string PngTaiji(int width = 100, int height = 100)
        => Png_(ResourceUtil.TaijiPicBase64());

        public static string PngBlack(int width = 100, int height = 100)
        => Png_(ImageUtil.BlackBase64());

        /// <summary>
        /// 关闭网页
        /// </summary>
        public const string CloseScript = "<script>window.close();</script><button onclick=\"javascript:window.close();\">close</button>";

    }
}
